<?php

namespace control;
/*
*订货订单
*/

class orderStoreClass extends adIndexClass
{

    private const page = 'orderStore';
    private const table = 'store_order';
    private const tableMx = 'store_order_detail';
    private const title = '订货订单';
    private const state = ['待下单', '待确认', '待发货', '已取消', '已发货', '已驳回'];

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        switch ($fun) {
            case 'clientOrderSearch':
                $tr = [
                    'orderNo'      => [
                        'name'  => '单据编号',
                        'width' => 200,
                        'sort'  => 1,
                    ],
                    'type'         => [
                        'name'  => "业务类型",
                        'width' => 200,
                        'sort'  => 1,
                    ],
                    'manager'      => [
                        'name'  => "销售员",
                        'width' => 200,
                    ],
                    'num'          => [
                        'name'  => '数量',
                        'width' => 150,
                    ],
                    'money'        => [
                        'name'  => '订单金额(' . static::$unit . '）',
                        'width' => 150,
                        'sort'  => 1,
                    ],
                    'deliveryDate' => [
                        'name'  => '交货日期',
                        'width' => 150,
                        'sort'  => 1,
                    ],
                    'recordDate'   => [
                        'name'  => '单据日期',
                        'width' => 150,
                        'sort'  => 1,
                    ],
                    'state'        => [
                        'name'  => '状态',
                        'width' => 150,
                    ],
                ];
                break;
            default:
                $tr = [
                    'orderNo'      => [
                        'name'  => '单据编号',
                        'width' => 200,
                        'sort'  => 1,
                    ],
                    'type'         => [
                        'name'  => "业务类型",
                        'width' => 200,
                        'sort'  => 1,
                    ],
                    'companyName'  => [
                        'name'  => "客户",
                        'width' => 200,
                    ],
                    'manager'      => [
                        'name'  => "销售员",
                        'width' => 150,
                    ],
                    'num'          => [
                        'name'  => '数量',
                        'width' => 150,
                    ],
                    'total'        => [
                        'name'  => '订单金额(' . static::$unit . '）',
                        'width' => 150,
                    ],
                    'deliveryDate' => [
                        'name'  => '交货日期',
                        'width' => 150,
                        'sort'  => 1,
                    ],
                    'recordDate'   => [
                        'name'  => '单据日期',
                        'width' => 150,
                        'sort'  => 1,
                    ],
                    'state'        => [
                        'name'  => '状态',
                        'width' => 150,
                    ],
                ];
        }
        return $tr;
    }

    /*列表页-查询条件*/
    public function clientOrderOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    "单据编号" => text(["name" => "orderNo"]) . hidden(["name" => "targetId", "value" => $this->get('id')]),
                    '销售员'   => selectMultiple(["name" => "stid", 'table' => " staff", 'sqlId' => 'stid', 'sqlField' => 'name', 'class' => 'width200']),
                    '单据日期' => text(["name" => "createTime", 'laydate' => ['type' => 'date', 'range' => '~']]),
                ],
                [
                    "状态" => select(["name" => "state", 'option' => ['草稿', '待确认', '已确认']]),
                ],
            ],
        ], 1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /**
     * 客户页卡销售单
     *
     * @return bool|string
     */
    public function clientOrder(): bool|string
    {
        $html   = $this->clientOrderOptions(false);
        $button = "";
        //返回
        $json['html'] = $this->search($html, layout: 'pop', toolButton: $button);
        return json_encode($json, 320);
    }

    public function clientOrderSearch(): bool|string
    {
        $khid = $this->post('targetId');
        //like查询数组
        $like = [
            "orderNo" => $this->post('orderNo'),
        ];
        //全等查询数组
        $equal = [
            "state" => $this->post('state'),
            "stid"  => $this->post('stid'),
            'coid'  => self::$coid,
        ];
        $and   = " and khid = '$khid'";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //排序
        $orderBy = $this->listSort([
            "search" => $this->post('list_sort_key') ?: 'recordDate',
            "sort"   => $this->post('list_sort') ?: "desc",
            "array"  => [],
        ]);
        //生成查询语句
        $where = search([
                "="    => $equal,
                "like" => $like,
            ]) . $and;
        $page  = $this->page(["table" => self::table, "where" => $where, "order" => $orderBy['orderBy']]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = [""];
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate   = [
                "detail" => ["title" => self::title . "详情", "click" => "newIframe('" . self::title . "详情','" . root . 'control/adIndex.html?cla=' . $this->get('cla') . '&fun=detail&id=' . $array['id'] . "')"],
            ];
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case "manager":
                        $row[] = self::findStaff($array['manager']);
                        break;
                    case "num":
                        $num   = find(['table' => self::tableMx, 'where' => "orderId = '$array[id]'", 'sum' => "num"]);
                        $row[] = $num;
                        break;
                    case "money":
                        $row[] = self::formattingFigure($array['money'], 'amount');
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //控件
        $button = "";
        //生成页面
        $li = $this->li([
            "body"        => $body,
            "install"     => $installSearch,
            "button"      => ["html" => $liButton],
            "orderBySign" => $orderBy,
            "page"        => $page['para'],
        ]);
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='contractForm'>" . $li . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    "状态" => checkbox(['name' => 'state', 'value' => self::state, 'checked' => ['待下单', '待确认']]),
                ],
                [
                    "单据编号" => text(["name" => "orderNo"]),
                    '客户名称' => selectMultiple(["name" => "khid", 'table' => " kehu", 'sqlId' => 'khid', 'where' => "coid = '" . self::$coid . "'", 'sqlField' => 'companyName', 'class' => 'width200']),
                    '销售员'   => selectMultiple(["name" => "manager", 'table' => " staff", 'sqlId' => 'stid', 'sqlField' => 'name', 'class' => 'width200']),
                ],
                [
                    "单据日期" => text(["name" => "orderDate", 'laydate' => ['type' => 'date', 'range' => '~']]),
                ],
            ],
        ], 1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }


    /*列表-查询条件*/
    public function home(): string
    {
        $html = $this->homeOptions(false);
        //控件
        $button = "";
        if ($this->power(self::page, "confirm")) {
            $button .= "<span onclick=\"layer('{$this->get('cla')}', 'operate', {'act': 'confirm'})\" class='spanButton'>批量确认</span>\n";
        }
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /*客户列表-查询结果*/
    public function homeSearch()
    {
        //like查询数组
        $like = [
            "orderNo" => $this->post('orderNo'),
        ];
        //全等查询数组
        $equal = [
            "stid"    => $this->post('stid'),
            "manager" => $this->post('manager'),
            'coid'    => self::$coid,
        ];
        $and   = '';
        if ($this->post('orderDate')) {
            $and .= self::timeComparison($this->post('orderDate'), 'date', 'orderDate');
        }
        if ($this->post('state')) {
            $and .= " AND state IN (" . whereIn($this->post('state')) . ") ";
        }
        //排序
        $orderBy = $this->listSort([
            "search" => $this->post('list_sort_key') ?: 'time',
            "sort"   => $this->post('list_sort') ?: "desc",
            "array"  => [],
        ]);
        //获取查询结果
        $where = search(["like" => $like, "=" => $equal]) . $and;
        $page  = $this->page(["table" => self::table, "where" => $where, "order" => $orderBy['orderBy']]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = [
            $this->checkAll("listForm", "listId"),
        ];
        //获取记录
        $url = root . "control/adIndex.html?cla=" . self::page;
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate   = [
                "box"    => ["name" => "listId", "value" => $array['id']],
                "detail" => ["title" => self::title . "详情", "click" => "newIframe('" . self::title . "详情','" . $url . "&fun=detail&id=" . $array['id'] . "')"],
            ];
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case "companyName":
                        $kehu  = query('kehu', "khid = '$array[khid]'");
                        $row[] = $kehu['companyName'];
                        break;
                    case "manager":
                        $row[] = self::findStaff($array['manager']);
                        break;
                    case "num":
                        $num   = find(['table' => self::tableMx, 'where' => "orderId = '$array[id]'", 'sum' => "num"]);
                        $row[] = self::lostZero($num, 'numPrecision');
                        break;
                    case "total":
                        $total = find(['table' => self::tableMx, 'where' => "orderId = '$array[id]'", 'sum' => "money"]);
                        $row[] = self::lostZero($total, 'amount');
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //控件
        $button = "";
        //生成页面
        $li = $this->li([
            "body"        => $body,
            "install"     => $installSearch,
            "button"      => ["html" => $liButton],
            "orderBySign" => $orderBy,
            "page"        => $page['para'],
        ]);
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . $li . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }


    /*详情页*/
    public function detail(): string
    {
        $info = query(self::table, " id = '{$this->get('id')}'");
        if (empty($info['id'])) {
            $html = '未找到此' . self::title;
        } else {
            //菜单
            $data   = [
                "id"   => $info['id'],
                'menu' => [
                    "detail"   => "基本信息",
                    'tracking' => '单据跟踪',
                ],
            ];
            $button = [];
            if ($info['state'] != '已确认') {
                if ($this->power(self::page, "edit")) {
                    if ($info['state'] == '草稿') {
                        //按钮
                        $button[] = [
                            "name"  => "提交",
                            "type"  => "弹出层",
                            "cla"   => $this->get('cla'),
                            "fun"   => "operate",
                            "post"  => ["act" => 'commit', "id" => $info['id']],
                            'style' => 'li-btn-blue-bg',
                        ];
                    }
                    //按钮
                    $button[] = [
                        "name"  => "编辑",
                        "type"  => "顶部页卡",
                        "title" => "编辑用户",
                        "url"   => $this->root . "control/adIndex.html?cla={$this->get('cla')}&fun=edit&id=" . $info['id'],

                    ];
                }
                if ($this->power(self::page, "del")) {
                    //按钮
                    $button[] = [
                        "name" => "删除",
                        "type" => "弹出层",
                        "cla"  => $this->get('cla'),
                        "fun"  => "operate",
                        "post" => ["act" => 'del', "id" => $info['id']],
                    ];
                }
            }
            if ($this->power(self::page, "confirm") && $info['state'] == '待确认') {
                $button[] = [
                    "name"  => "确认",
                    "type"  => "弹出层",
                    "cla"   => $this->get('cla'),
                    "fun"   => "operate",
                    "post"  => ["act" => 'confirm', "id" => $info['id']],
                    'style' => 'li-btn-blue-bg',
                ];
            }
            if ($this->power(self::page, "cancel") && $info['state'] == '已确认' && !$info['isClose']) {
                $button[] = [
                    "name" => "撤回",
                    "type" => "弹出层",
                    "cla"  => $this->get('cla'),
                    "fun"  => "operate",
                    "post" => ["act" => 'cancel', "id" => $info['id']],
                ];
            }
            if ($this->power(self::page, "edit") && $info['state'] == '已确认') {
                //按钮
                $isClose  = $info['isClose'] == 1 ? 0 : 1;
                $name     = $info['isClose'] == 1 ? '开启' : '关闭';
                $button[] = [
                    "name" => $name,
                    "type" => "点击事件",
                    "fun"  => "layer('" . self::page . "','stateSwitch',{id:'" . $info['id'] . "',type:'$isClose'})",
                ];
            }
            //操作记录
            $button[] = self::operateLogBut($info['id']);

            $kehu   = query('kehu', "khid = '$info[khid]'");
            $tr[]   = ['产品编码', '产品名称', '客户料号', '客户品名', '规格型号', '单位', '数量', '单价(' . static::$unit . ')', '金额（' . static::$unit . '）', '税率%', '税额（' . static::$unit . '）', '价税合计（' . static::$unit . '）', '备注'];
            $result = find(['table' => self::tableMx, 'where' => "orderId = '$info[id]' order by list"]);
            foreach ($result['array'] as $array) {
                $material    = query('material', " id = '$array[materialId]'");
                $clientPrice = query('store_client_price', generateWhere([
                    'coid'       => self::$coid,
                    'khid'       => $info['khid'],
                    'materialId' => $array['materialId'],
                ]));
                $tr[]        = [
                    $material['code'],
                    $material['name'],
                    $clientPrice['materialAlias'],
                    $clientPrice['materialAliasNo'],
                    $material['model'],
                    $material['unit'],
                    self::lostZero($array['num'], 'numPrecision'),
                    self::lostZero($array['price'], 'pricePrecision'),
                    self::lostZero($array['num'] * $array['price']),
                    ($array['rate'] * 100) . '%',
                    self::formattingFigure($array['rate'] * $array['money']),
                    self::formattingFigure((1 + $array['rate']) * $array['money']),
                    $array['text'],
                ];
            }
            $total               = find([
                'table'  => self::tableMx,
                'field'  => " sum(any_value(num)) as num, sum(any_value(money)) as money, sum(any_value(price)*any_value(num)) as money, sum(any_value(money * rate)) as taxMoney, sum(any_value(money * (1+rate))) as totalMoney",
                'single' => " orderId = '$info[id]' group by orderId",
            ]);
            $total['num']        = $total['num'] ?? 0;
            $total['money']      = $total['money'] ?? 0;
            $total['totalMoney'] = $total['totalMoney'] ?? 0;
            $total['taxMoney']   = $total['taxMoney'] ?? 0;
            //生成表单
            $data['html'] = $this->tableShow([
                "button"   => $button,
                [
                    [
                        "业务类型" => $info['type'],
                        "客户"     => $kehu['companyName'],
                    ],
                    [
                        "收货人"   => $info['name'],
                        "收货电话" => $info['tel'],
                        "收货地址" => $info['address'],
                    ],
                    [
                        "单据日期" => self::null($info['orderDate']),
                        "交货日期" => self::null($info['deliveryDate']),
                    ],
                    [
                        "备注" => $info['text'],
                    ],
                    [
                        "单据编号" => $info['orderNo'],
                        "制单人"   => self::findStaff($info['stid']),
                        "状态"     => $info['state'],
                    ],
                ],
                '订单明细' => [
                    [
                        "<div style='color: grey;float: right;margin-right: 20px'>
                             &emsp;数量合计：" . self::lostZero($total['num'], 'numPrecision') .
                        "&emsp;金额合计：" . self::lostZero($total['money'], 'amount') .
                        "&emsp;税额合计：" . self::lostZero($total['taxMoney'], 'amount') .
                        "&emsp;价税合计：" . self::lostZero($total['totalMoney'], 'amount') . "
                            </div>" .
                        $this->tableMany(['tr' => $tr]),
                    ],
                ],
            ]);


            //生成页面
            $html = $this->tab($data);
        }
        //返回
        return $html;
    }


    /**
     * 单据追踪
     *
     * @return string
     */
    function tracking(): string
    {
        $info = query(self::table, " id = '{$this->get('id')}' ");
        // 销售
        $sTr[] = ['单据日期', '销售订单号', '合同编号', '客户', '销售员', '产品编码', '产品名称', '规格型号', '属性', '单位', '销售数量', '发货数量', '未发货数量', '交货日期', '最后出库日期', '状态'];
        $sInfo = query('sale_order', generateWhere([
            'coid'       => self::$coid,
            'contractId' => $info['id'],
        ]));
        // if($sInfo['id']){
        // 	$sResult = find([
        // 		'table' => 'sale_order',
        // 		'where' => generateWhere([
        // 			'coid'       => self::$coid,
        // 			'orderId' => $sInfo['id'],
        // 		]),
        // 	]);
        // }

        // 采购
        $pTr[] = ['单据日期', '采购订单号', '合同编号', '供应商', '物料编码', '物料名称', '规格型号', '属性', '单位', '采购数量', '收货数量', '未收货数量', '交货日期', '最后入库日期', '状态'];
        // $pResult = find([
        // 	'table' => 'sale_order',
        // 	'where' => '',
        // ]);
        // 生产
        $prTr[] = ['单据日期', '生产派工单号', '生产线', '工序', '物料编码', '物料名称', '规格型号', '属性', '单位', '计划生产数量', '完工数量', '未完工数量', '计划完工时间', '最后入库日期', '状态'];
        // $prResult = find([
        // 	'table' => 'sale_order',
        // 	'where' => '',
        // ]);
        $html = $this->tableShow([
            '订单编号' => [
                [
                    $info['orderNo'],
                ],
            ],
            '销售'     => [
                [
                    $this->tableMany(['tr' => $sTr,]),
                ],
            ],
            '采购'     => [
                [
                    $this->tableMany(['tr' => $pTr,]),
                ],
            ],
            '生产'     => [
                [
                    $this->tableMany(['tr' => $prTr,]),
                ],
            ],
        ]);
        return json_encode(['html' => $html], 320);
    }

    /**
     * 删除
     *
     * @return false|string
     */
    public function delete()
    {
        $array = $this->post('listId') ?? $this->post('id');
        if (!$this->power(self::page, "del")) {
            $json['warn'] = "你没有删除的权限";
        } elseif (empty($array)) {
            $json['warn'] = "你一个数据都没有选择呢";
        } else {
            $text  = "";
            $x     = 0;
            $array = is_array($array) ? $array : [$array];
            foreach ($array as $id) {
                $info = query(self::table, " coid = '" . self::$coid . "' AND id = '$id' ");
                if ($info['state'] == '已确认') {
                    $warn = "删除失败：已确认，暂不能删除";
                } else {
                    //最后删除基本参数
                    delete(["table" => self::table, "where" => " id = '$id' "]);
                    //返回
                    $warn = "删除成功";
                    $x++;
                }
                $text .= self::title . "ID【" . $info['id'] . "】" . $warn . "。<br>";
            }
            //添加日志
            $text = $this->control['name'] . "删除了" . self::title . "，结果如下：<br>" . $text;
            $this->logText(["type" => self::page, "text" => $text]);
            $_SESSION['warn'] = "删除了" . $x . "个" . self::title . "，详情见系统日志。";
            $json['warn']     = 2;
            $json['href']     = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=home";
        }
        return json_encode($json, 320);
    }

    /**
     * 确认
     */
    public function confirmEdit()
    {
        $array = $this->post('listId') ?? $this->post('id');
        if (!$this->power(self::page, "confirm")) {
            $json['warn'] = "你没有确认的权限";
        } elseif (empty($array)) {
            $json['warn'] = "你一个数据都没有选择呢";
        } else {
            $text  = "";
            $x     = 0;
            $array = is_array($array) ? $array : [$array];
            foreach ($array as $id) {
                $info = query(self::table, " id = '$id'");
                if (empty($info['id'])) {
                    continue;
                }
                if ($info['state'] != '待确认') {
                    continue;
                }
                $this->startTrans();
                $json['warn'] = update([
                    "table" => self::table,
                    "field" => [
                        'state' => '已确认',
                    ],
                    "where" => "id = '$info[id]'",
                ]);
                if (in_array($json['warn'], ["新增成功", "更新成功"])) {
                    // 写入到销售订单
                    $orderNo = codeTemplateClass::generateCode(self::page, [
                        'prefix' => 'XSDD' . date('Ymd', strtotime($info['orderNo'])),
                        'table'  => self::table,
                        'field'  => 'orderNo',
                        'where'  => " and coid = '" . self::$coid . "'",
                    ], []);
                    $sid     = self::getId();
                    static::insert([
                        'table' => 'sale_order',
                        'field' => [
                            'id'              => $sid,
                            'coid'            => self::$coid,
                            'orderNo'         => $orderNo,
                            'type'            => '订货',
                            'khid'            => $info['khid'],
                            'manager'         => $info['manager'],
                            'contractId'      => $info['id'],
                            'recordDate'      => $info['orderDate'],
                            'deliveryDate'    => '',
                            'text'            => $info['text'],
                            'contactName'     => $info['name'],
                            'contactTel'      => $info['tel'],
                            'address'         => $info['address'],
                            'state'           => '已确认',
                            'examine'         => '',
                            'stid'            => self::$stid,
                            'isTax'           => '1',
                            'money'           => $info['money'],
                            'delivery_method' => '2',
                            'settlementType'  => '',
                            'settlementDay'   => '',
                            'tags'            => '',
                            'checkId'         => self::$stid,
                            'isClose'         => 0,
                            'confirmTime'     => $this->time,
                        ],
                    ]);
                    $result = find([
                        'table' => self::tableMx,
                        'where' => generateWhere([
                            'coid'    => self::$coid,
                            'orderId' => $info['id'],
                        ]),
                    ]);
                    foreach ($result['array'] as $array) {
                        $storeClientPrice = query('store_client_price', "khid = '$info[khid]' and materialId = '$array[materialId]' order by time desc");
                        static::insert([
                            'table' => 'sale_order_detail',
                            'field' => [
                                'id'                 => self::getId(),
                                'orderId'            => $sid,
                                'coid'               => self::$coid,
                                'contract_detail_id' => $array['id'],
                                'khMaterialName'     => $storeClientPrice['materialAlias'],
                                'khMaterialCode'     => $storeClientPrice['materialAliasNo'],
                                'materialId'         => $array['materialId'],
                                'num'                => self::formattingFigure($array['num'], 'numPrecision'),
                                'price'              => self::formattingFigure($array['price'], 'pricePrecision'),
                                'taxMoney'           => self::formattingFigure($array['num'] * $array['price'] * $array['rate'], 'amount'),
                                'moneyWithOutTax'    => self::formattingFigure($array['num'] * $array['price'], 'amount'),
                                'money'              => self::formattingFigure($array['num'] * $array['price'] * (1 + $array['rate']), 'amount'),
                                'rate'               => $array['rate'],
                                'supplierId'         => '',
                                'text'               => $array['text'],
                            ],
                        ]);
                    }
                    //生成产品需求单
                    $bool = saleOrderClass::product_demands($sid);
                    if ($bool !== true) {
                        $json['warn'] = $bool;
                    }
                    //生成外购需求单
                    $bool = saleOrderClass::purchase_demands($sid);
                    if ($bool !== true) {
                        $json['warn'] = $bool;
                    }
                    //生成委外需求单
                    $bool = saleOrderClass::outsource_demands($sid);
                    if ($bool !== true) {
                        $json['warn'] = $bool;
                    }
                }

                if (in_array($json['warn'], ["新增成功", "更新成功"])) {
                    $this->commitTrans();
                    auditClass::operateRecord($id, '销售单', '确认');
                    //添加日志
                    $type = mb_substr($json['warn'], 0, 2, 'utf-8');
                    $text = $this->control['name'] . $type . "了" . self::title . "id：【" . $id . '】';
                    $this->logText(["type" => self::page, "text" => $text]);
                    //返回
                    $_SESSION['warn'] = $json['warn'];
                    $json['warn']     = 2;
                    $json['href']     = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=detail&id=" . $info['id'];
                } else {
                    $this->rollback();
                }

                // $info = query(self::table, " coid = '" . self::$coid . "' AND id = '$id' ");
                // if ($info['state'] == '已审核') {
                // 	$warn = "删除失败：已审核，暂不能删除";
                // } else {
                // 	//最后删除基本参数
                // 	delete(["table" => self::table, "where" => " id = '$id' "]);
                // 	//返回
                // 	$warn = "删除成功";
                // 	$x++;
                // }
                // $text .= self::title . "ID【" . $info['id'] . "】" . $warn . "。<br>";
            }
            //添加日志
            $text = $this->control['name'] . "确认了" . self::title . "，结果如下：<br>" . $text;
            $this->logText(["type" => self::page, "text" => $text]);
            $_SESSION['warn'] = "确认了" . $x . "个" . self::title . "，详情见系统日志。";
            $json['warn']     = 2;
            $json['href']     = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=home";
        }
        return json_encode($json, 320);
    }

    // 操作中转
    public function operate()
    {
        $act  = $this->post('act') ?? '';
        $pwd  = $word = false; // 需要密码
        $form = $power = '';
        switch ($act) {
            case 'confirm':
                $act      = 'confirm';
                $msg      = '确认';
                $title    = '是否已确认此销售单，确认后将不可修改？';
                $function = 'confirmEdit';
                $form     = self::page . 'OperateForm, detailForm, listForm';
                $power    = '';
                $word     = true;
                break;
            case 'commit':
                $act      = 'edit';
                $msg      = '编辑';
                $title    = '是否确认提交此销售单？';
                $function = 'commitEdit';
                $form     = self::page . 'OperateForm, detailForm';
                $power    = '';
                $word     = true;
                break;
            case 'cancel':
                $act      = 'cancel';
                $msg      = '撤回';
                $title    = '是否确认撤回此销售单？';
                $function = 'cancelEdit';
                $form     = self::page . 'OperateForm, detailForm';
                $power    = '';
                $word     = true;
                break;
            default:
                $act      = 'del';
                $msg      = '删除' . self::title;
                $title    = self::page . 'Delete';
                $function = 'delete';
                $form     = self::page . 'OperateForm, listForm, quoForm';
                $power    = '';
                break;
        }
        if (!$this->power(self::page, $act)) {
            $_SESSION['warn'] = '你还没有' . $msg . '的权限';
            $json             = [
                'warn' => 2,
                'href' => root . 'control/adIndex.html?cla=' . $this->get('cla') . '&fun=home',
            ];
        } else {
            return $this->confirm([
                'title'    => $title,
                'function' => $function,
                'form'     => $form,
                'power'    => $power,
                'pwd'      => $pwd,
                'word'     => $word,
            ]);
        }

        resp:
        return json_encode($json, 320);
    }

}
